*WHAT'S ACROSS THE BORDER?
*JHA, NEUMARK, AND RODRIGUEZ-LOPEZ

include directories.do


***********************************************
*Regressions for period 1990-upto
local upto 2016
***********************************************

foreach u in "czonestate" "county" {
* Load stacked pair data at the county or czone-state level
use "`datadir'`s'cbp_stacked_`u'pair_sample.dta", clear

if "`u'" == "county" {
merge m:1 countypair_id county using "`countydir'`s'countypairtypes.dta"
keep if type0==1
drop type*
drop _merge
}

************************************************
*******COUNTY-PAIRS OR MULTI-STATA CZONE PAIRS
************************************************

*** PAIR ID VARIABLE (EITHER CZONESTATEPAIR OR COUNTYPAIR)
local pair `u'pair


*Entity variable (EITHER CZONESTATE OR COUNTY)
local geo `u'

***********************************************
*GENERATE VARIABLES (SAME FOR COUNTY OR CZONE)
***********************************************


gen lmw=log(mw)

*Generate totempm, which removes emp of the industry of interest
foreach b in "emp" {
gen tot`b'm=tot`b'-`b'
}

foreach b in "emp" "wageap" {

gen l`b'=log(`b')
gen ltot`b'=log(tot`b')
gen ltot`b'm=log(tot`b'm)
}

gen lworkagepop=log(workagepop)


********************************************
**** DEFINE LOCAL VARIABLES
********************************************

local period year

*Dependent variable and regressors (SAME FOR COUNTY OR CZONE)

local mwvar lmw
local popvar lworkagepop
local wagevar lwageap
local empvar lemp
local totempvar ltotempm
local totwagevar ltotwageapm



**************************************************
****STACKED REGRESSIONS WITH REGHDFE
**************************************************

sort  `pair' `period'
 
gen state_a = statea
gen state_b = stateb

gen st_min = min(state_a, state_b)
gen st_max = max(state_a, state_b)
egen bordersegment = group(st_min st_max)
egen pairperiod=group(`pair' `period')

*USE DATA UP 1990 TO 2006
keep if `period'>=1990 & `period'<=`upto'

*Specify clustering
local options "vce(cluster state bordersegment) absorb(`u' pairperiod)"

*Specifications


foreach h in "emp" "wage" {

local controls "`tot`h'var' `popvar'"
local entityfe "i.`u'"

eststo m`u'_`h'_1: reghdfe ``h'var' `mwvar' `controls', `options'

eststo m`u'_`h'_2: reghdfe ``h'var' `mwvar' `controls' if pairtimes==2,  `options'
}


}


/* Print results */

tempfile specs

#delimit ;
esttab mczonestate_emp* mcounty_emp* using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("Pair approach estimation -- employment")
	keep(`mwvar' `totempvar' `popvar')
	nonotes
	label
	stats(N, labels("N") fmt(0))	
	replace;
#delimit cr

#delimit ;
esttab mczonestate_wage* mcounty_wage* using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("Pair approach estimation -- earnings")
	keep(`mwvar' `totwagevar' `popvar')
	nonotes
	label
	stats(N, labels("N") fmt(0))	
	append;
#delimit cr

* Switch the output to Excel format
insheet using "`specs'.csv", comma clear
export excel "`outdir'`s'TablesJNR.xlsx", sheetreplace sheet(Table3)

clear all


